package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategorySevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class CategoryIServicempl implements CategorySevice {
    @Autowired
    private CategoryMapper  categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Transactional
    public boolean save(CategoryDTO categoryDTO) {
        //判断排序是否重复
        if(!categoryMapper.judgeSortIsExist(categoryDTO).isEmpty()){
            return false;
        }
        //判断分类名称是否重复
        if(!categoryMapper.judgeNameIsExist(categoryDTO).isEmpty()){
            return false;
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置默认状态为禁用
        category.setStatus(0);
        //设置时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //设置创建人和修改人
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.addCategory(category);
        return true;
    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    public PageResult pagequery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    public boolean delete(Integer id) {
        if(!dishMapper.getDishByCateId(id).isEmpty()){
            return false;
        }
        categoryMapper.DeleteById(id);
        return true;
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    public void changeStatus(Integer status, long id) {
        Category category = categoryMapper.getCategoryById(id);
        category.setStatus(status);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.changeStatus(category);
    }

    /**(
     * 修改分类
     * @param categoryDTO
     */
    public boolean editCategory(CategoryDTO categoryDTO) {
        //判断排序是否重复
        if(!categoryMapper.judgeSortIsExist(categoryDTO).isEmpty()){
            return false;
        }
        //判断分类名称是否重复
        if(!categoryMapper.judgeNameIsExist(categoryDTO).isEmpty()){
            return false;
        }
        Category category = categoryMapper.getCategoryById(categoryDTO.getId());
        //设置修改的内容
        category.setName(categoryDTO.getName());
        category.setSort(categoryDTO.getSort());
        //设置时间,修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.editCategory(category);
        return true;
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<Category> getCategoryByType(Integer type) {
        return categoryMapper.getCategoryByType(type);
    }
}
